//
//  SettingViewController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/10/26.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "SettingViewController.h"
#import "AboutUsController.h"
#import "MessageSettingController.h"
#import "AccountSafeController.h"
#import "PersonalInformationController.h"

@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic , strong) UITableView * tableview;

@property (nonatomic , strong) NSArray * titleArray;

@property (nonatomic , strong) UILabel * rightLabel;

@end

@implementation SettingViewController

-(NSArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = @[@"个人资料",@"账户安全",@"消息推送设置",@"清除缓存"];
    }
    return _titleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)setNavi
{
    self.title = @"设置";
}

- (void)setUI
{
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 60) style:(UITableViewStyleGrouped)];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableview];
    
    
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    }else
    {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 70)];
        view.userInteractionEnabled = YES;
        view.backgroundColor = [UIColor clearColor];
        UIButton * btn = [[UIButton alloc]init];
        [btn setBackgroundColor:[UIColor whiteColor]];
        btn.frame = CGRectMake(0, 20, kScreenWidth, 50);
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [btn setTitle:@"退出登录" forState:(UIControlStateNormal)];
        [btn setTitleColor:TextMainColor forState:(UIControlStateNormal)];
        [btn addTarget:self action:@selector(logout) forControlEvents:(UIControlEventTouchUpInside)];
        [view addSubview:btn];
        return view;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01f;
    }else
    {
        return 70;
    }
}

//@property (nonatomic , strong) NSString * nickName;       // 用户名
//@property (nonatomic , strong) NSString * telephoneNumber;    // 电话号码
//@property (nonatomic , strong) NSString * password;    // 密码
//@property (nonatomic , strong) NSString * brithDay;  // 生日
//@property (nonatomic , strong) NSString * payForPassword;  // 支付密码
//@property (nonatomic , strong) NSString * redisKey;  // 缓存的key
//@property (nonatomic , strong) NSString * userToken;  // token
//@property (nonatomic , strong) NSString * QRcodeUrl;   // 二维码图片url
//@property (nonatomic , strong) NSString * servicePhoneNum; // 客服电话
//@property (nonatomic , strong) NSString * userImageString; // 用户头像url
//@property (nonatomic , assign) NSInteger gender;  // 性别  0未设置 1男 2女
//@property (nonatomic , assign) NSInteger region_id; // 地区
//@property (nonatomic , assign) NSInteger actuallyName; // 实名认证 0 未认证 1 正在审核 2 审核通过 3审核拒绝
//@property (nonatomic , strong) NSString * starPhoneNumber;  // 带星号的电话号码
//@property (nonatomic , strong) NSString * accountMoney;

- (void)logout
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/logout",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            self.isLogin = NO;
            self.nickName = @"";
            self.telephoneNumber = @"";
            self.password = @"";
            self.brithDay = @"";
            self.payForPassword = @"";
            self.redisKey = @"";
            self.userToken = @"";
            self.QRcodeUrl = @"";
            self.servicePhoneNum = @"";
            self.userImageString = @"";
            self.starPhoneNumber = @"";
            self.accountMoney = @"";
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 4;
    }else
    {
        return 1;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.textColor = TextAssistColor;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.section == 0) {
        cell.textLabel.text = self.titleArray[indexPath.row];
        if (indexPath.row == 3) {
            cell.accessoryType = UITableViewCellAccessoryNone;
            self.rightLabel = [[UILabel alloc]init];
            self.rightLabel.text = [self getSize];
            self.rightLabel.textColor = TextLessImportColor;
            [cell.contentView addSubview:self.rightLabel];
            [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.offset(-20);
                make.centerY.equalTo(cell.contentView);
            }];
        }
    }else
    {
        cell.textLabel.text = @"关于无界";
    }
    
    return cell;
}

- (NSString *)getSize
{
    //得到系统缓存大小
    double cacheSize = [SDImageCache sharedImageCache].getSize;
    NSString * cacheSizeString = nil;
    if (cacheSize == 0) {
        cacheSizeString = @"0B";
    }
    else if (cacheSize < 1024) {
        cacheSizeString = [NSString stringWithFormat:@"%.2fB",cacheSize];
    }
    else if (cacheSize < 1024 * 1024){
        cacheSizeString = [NSString stringWithFormat:@"%.2fKB",cacheSize/1024];
    }
    else if (cacheSize < 1024 * 1024 * 1024){
        cacheSizeString = [NSString stringWithFormat:@"%.2fMB",cacheSize/(1024 * 1024)];
    }
    else{
        cacheSizeString = @"大于1GB";
    }
    return cacheSizeString;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                [self toPersonalInformation];
            }
                break;
            case 1:
            {
                [self toAccoutSafe];
            }
                break;
            case 2:
            {
                [self toMessage];
            }
                break;
            case 3:
            {
                [self clearMemory];
            }
                break;
                
            default:
                break;
        }
    }else if (indexPath.section == 1){
        [self toAboutUs];
    }
}

#pragma mark 关于我们
- (void)toAboutUs
{
    AboutUsController * vc = [[AboutUsController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 个人资料
-(void)toPersonalInformation
{
    PersonalInformationController * vc = [[PersonalInformationController alloc]initWithNibName:@"PersonalInformationController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 账户安全
- (void)toAccoutSafe
{
    AccountSafeController * vc = [[AccountSafeController alloc]initWithNibName:@"AccountSafeController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 消息推送
- (void)toMessage
{
    MessageSettingController * vc = [[MessageSettingController alloc]initWithNibName:@"MessageSettingController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 清除缓存
- (void)clearMemory
{
    WS(weakSelf)
    UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否清除缓存" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            [MBProgressHUD showSuccess:@"清除成功" toView:self.tableview];
            weakSelf.rightLabel.text = @"0B";
        }]; 
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:okAction];
    [self presentViewController:alertVC animated:YES completion:nil];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
